﻿using AutoMapper;
using MediatR;
using MyMES.Domain.zyy;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Writer.API.Application.Commends.Works;

namespace MyMES.Writer.API.Application.CommendHandlers.Works
{
    public class AddWorkHandler : IRequestHandler<AddWorksCommands, APIResult<WorkOrder>>
    {
        private readonly IBaseRepository<WorkOrder> repository;
        private readonly IMapper mapper;

        public AddWorkHandler(IBaseRepository<WorkOrder> repository,IMapper mapper )
        {
            this.repository = repository;
            this.mapper = mapper;
        }
        /// <summary>
        /// 添加生产公单
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>

        public async Task<APIResult<WorkOrder>> Handle(AddWorksCommands request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<WorkOrder> result = new APIResult<WorkOrder>();
                result.Code = APIEnum.添加成功;
                result.Msg = "添加成功";
                var model = mapper.Map<WorkOrder>(request);
                string sql = "insert WorkOrder (WorkOrderCoding,WorkOrderName,WorkOrderType,WorkorderSource,OrderNumber,ProductNumber,ProductName,Specifications,Unit,WorkOrderNum,AdjustQuantity,QuantityProduced,LotNumber,CustomerCode,CustomerNamwe,RequestDate,State,SupplierCode,SupplierName,Note,IsDlete) values(@WorkOrderCoding,@WorkOrderName,@WorkOrderType,@WorkorderSource,@OrderNumber,@ProductNumber,@ProductName,@Specifications,@Unit,@WorkOrderNum,@AdjustQuantity,@QuantityProduced,@LotNumber,@CustomerCode,@CustomerNamwe,@RequestDate,@State,@SupplierCode,@SupplierName,@Note,@IsDlete)";
                await repository.Insert(model,sql);
                return result;
            }
            catch (Exception)
            {

                throw;
            }
           
        }
    }
}
